/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.base;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewStub;

import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseSystemActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.QQAccessToken;
import com.simon.mengkou.data.bean.base.SinaAccessToken;
import com.simon.mengkou.data.bean.base.WeixinAccessToken;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.utils.UtilOuer;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.Map;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/5.
 * desc    :扭蛋拉新基类界面
 */
public abstract class BaseRulesActivity extends BaseSystemActivity{
    //友盟社交api
    private UMShareAPI mShareAPI;
    private ShareAction mShareAction;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mShareAPI = UMShareAPI.get(this);
    }


    @Override
    protected void initSystem() {
        //禁用设置状态栏背景颜色
        setSystemBarEnabled(false);
    }


    @Override
    public void setContentView(@LayoutRes int layoutResId) {
        super.setContentView(R.layout.activity_base_rules);

        //显示内容
        ViewStub stub = (ViewStub)findViewById(R.id.rules_id_stub);
        stub.setLayoutResource(layoutResId);
        stub.inflate();

        //返回按钮
        findViewById(R.id.rules_id_navigation).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        if(v.getId() == R.id.rules_id_navigation) {//返回
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 新浪分享
     * @param title
     * @param text
     * @param targetUrl
     * @param imgUrl
     */
    public void share(SHARE_MEDIA platform, String title, String text, String targetUrl, String imgUrl) {
        if(UtilString.isNotBlank(imgUrl)) {
            mShareAction = new ShareAction(this)
                    .setPlatform(platform)
                    .setCallback(umShareListener)
                    .withText(text)
                    .withTargetUrl(targetUrl)
                    .withMedia(new UMImage(this, imgUrl));
        } else {
            mShareAction = new ShareAction(this)
                    .setPlatform(platform)
                    .setCallback(umShareListener)
                    .withText(text)
                    .withTargetUrl(targetUrl);
        }

        if(mShareAPI.isAuthorize(this, platform)) {
            if(platform == SHARE_MEDIA.SINA) {
                SinaAccessToken token = SinaAccessToken.readAccessToken(this);
                if(token != null && token.isSessionValid() && !token.isExpires()) {
                    mShareAction.share();
                } else {
                    mShareAPI.doOauthVerify(this, platform, umAuthListener);
                }
            } else if(platform == SHARE_MEDIA.QQ
                    || platform == SHARE_MEDIA.QZONE) {
                QQAccessToken token = QQAccessToken.readAccessToken(this);
                if(token != null && token.isSessionValid() && !token.isExpires()) {
                    mShareAction.share();
                } else {
                    mShareAPI.doOauthVerify(this, platform, umAuthListener);
                }
            } else if(platform == SHARE_MEDIA.WEIXIN
                    || platform == SHARE_MEDIA.WEIXIN_CIRCLE) {
                WeixinAccessToken token = WeixinAccessToken.readAccessToken(this);
                if(token != null && token.isSessionValid() && !token.isExpires()) {
                    mShareAction.share();
                } else {
                    mShareAPI.doOauthVerify(this, platform, umAuthListener);
                }
            }

        } else {
            mShareAPI.doOauthVerify(this, platform, umAuthListener);
        }

    }

    /**
     * 新浪分享
     * @param title
     * @param text
     * @param targetUrl
     * @param bitmap
     */
    public void share(SHARE_MEDIA platform, String title, String text, String targetUrl, Bitmap bitmap) {
        if(bitmap != null) {
            mShareAction = new ShareAction(this)
                    .setPlatform(platform)
                    .setCallback(umShareListener)
                    .withText(text)
                    .withTargetUrl(targetUrl)
                    .withMedia(new UMImage(this, bitmap));
        } else {
            mShareAction = new ShareAction(this)
                    .setPlatform(platform)
                    .setCallback(umShareListener)
                    .withText(text)
                    .withTargetUrl(targetUrl);
        }

        if(mShareAPI.isAuthorize(this, platform)) {
            if(platform == SHARE_MEDIA.SINA) {
                SinaAccessToken token = SinaAccessToken.readAccessToken(this);
                if(token != null && token.isSessionValid() && !token.isExpires()) {
                    mShareAction.share();
                } else {
                    mShareAPI.doOauthVerify(this, platform, umAuthListener);
                }
            } else if(platform == SHARE_MEDIA.QQ
                    || platform == SHARE_MEDIA.QZONE) {
                QQAccessToken token = QQAccessToken.readAccessToken(this);
                if(token != null && token.isSessionValid() && !token.isExpires()) {
                    mShareAction.share();
                } else {
                    mShareAPI.doOauthVerify(this, platform, umAuthListener);
                }
            } else if(platform == SHARE_MEDIA.WEIXIN
                    || platform == SHARE_MEDIA.WEIXIN_CIRCLE) {
                WeixinAccessToken token = WeixinAccessToken.readAccessToken(this);
                if(token != null && token.isSessionValid() && !token.isExpires()) {
                    mShareAction.share();
                } else {
                    mShareAPI.doOauthVerify(this, platform, umAuthListener);
                }
            }
        } else {
            mShareAPI.doOauthVerify(this, platform, umAuthListener);
        }

    }

    /**
     * 分享监听
     */
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            UtilOuer.toast(BaseRulesActivity.this, R.string.common_share_success);
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            UtilOuer.toast(BaseRulesActivity.this, R.string.common_share_failure);

            if(platform == SHARE_MEDIA.SINA) {
                SinaAccessToken.clear(BaseRulesActivity.this);
            } else if(platform == SHARE_MEDIA.QQ
                    || platform == SHARE_MEDIA.QZONE) {
                QQAccessToken.clear(BaseRulesActivity.this);
            } else if(platform == SHARE_MEDIA.WEIXIN
                    || platform == SHARE_MEDIA.WEIXIN_CIRCLE) {
                WeixinAccessToken.clear(BaseRulesActivity.this);
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {

        }
    };



    /**
     * 授权监听
     */
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            if(platform == SHARE_MEDIA.SINA) {//新浪
                if(data != null) {
                    SinaAccessToken token = new SinaAccessToken();
                    token.setUid(data.get(SinaAccessToken.KEY_UID));
                    token.setAccessToken(data.get(SinaAccessToken.KEY_ACCESS_TOKEN));
                    token.setRefreshToken(data.get(SinaAccessToken.KEY_REFRESH_TOKEN));
                    token.setExpiresIn(data.get(SinaAccessToken.KEY_EXPIRES_IN));

                    if(token.isSessionValid()) {
                        SinaAccessToken.writeAccessToken(BaseRulesActivity.this, token);
                        mShareAction.share();
                    } else {
                        UtilOuer.toast(BaseRulesActivity.this, R.string.common_share_failure);
                    }
                }
            } else if(platform == SHARE_MEDIA.QQ || platform == SHARE_MEDIA.QZONE) {//qq
                if(data != null) {
                    QQAccessToken token = new QQAccessToken();
                    token.setUid(data.get(QQAccessToken.KEY_UID));
                    token.setAccessToken(data.get(QQAccessToken.KEY_ACCESS_TOKEN));
                    token.setAppId(CstOuer.TENCENT.TENCENT_APP_ID);
                    token.setExpiresIn(data.get(QQAccessToken.KEY_EXPIRES_IN));
                    token.setOpenId(data.get(QQAccessToken.KEY_OPENID));

                    if(token.isSessionValid()) {
                        QQAccessToken.writeAccessToken(BaseRulesActivity.this, token);
                        mShareAction.share();
                    } else {
                        UtilOuer.toast(BaseRulesActivity.this, R.string.common_share_failure);
                    }
                }
            } else if(platform == SHARE_MEDIA.WEIXIN|| platform == SHARE_MEDIA.WEIXIN_CIRCLE) {//微信
                if(data != null) {
                    WeixinAccessToken token = new WeixinAccessToken();
                    token.setOpenId(data.get(WeixinAccessToken.KEY_OPENID));
                    token.setAccessToken(data.get(WeixinAccessToken.KEY_ACCESS_TOKEN));
                    token.setRefreshToken(data.get(WeixinAccessToken.KEY_REFRESH_TOKEN));
                    token.setExpiresIn(data.get(WeixinAccessToken.KEY_EXPIRES_IN));
                    token.setUnionId(data.get(WeixinAccessToken.KEY_UNIONID));
                    token.setScope(data.get(WeixinAccessToken.KEY_SCOPE));

                    if(token.isSessionValid()) {
                        WeixinAccessToken.writeAccessToken(BaseRulesActivity.this, token);
                        mShareAction.share();
                    } else {
                        UtilOuer.toast(BaseRulesActivity.this, R.string.common_share_failure);
                    }
                }
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            UtilOuer.toast(BaseRulesActivity.this, R.string.common_share_failure);
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {

        }
    };

}
